package fr.xgouchet.wedding.client.view;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import fr.xgouchet.wedding.client.i18n.Constants;
import fr.xgouchet.wedding.client.i18n.Messages;
import fr.xgouchet.wedding.client.presenter.GuestsListPresenter.FormDisplay;
import fr.xgouchet.wedding.client.service.GuestsService;
import fr.xgouchet.wedding.shared.model.Guest;

/**
 * UI Widget to display a form to create/edit a guest
 * 
 * @author x.gouchet
 * 
 */
public class GuestFormView extends DialogBox implements FormDisplay {

	/**
	 * Default Constructor
	 */
	public GuestFormView() {
		// Set the dialog box's caption.
		setText(Constants.I18N.guestFormCaption());
		setModal(true);

		// create buttons
		mSaveButton = new Button(Constants.I18N.buttonSave());
		mCancelButton = new Button(Constants.I18N.buttonCancel());
		Button add = new Button(Constants.I18N.buttonAdd());
		Button clear = new Button(Constants.I18N.buttonClear());

		// Local handlers
		add.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				String roles = mRolesBox.getValue() + ","
						+ mRolesBoxInput.getValue();
				mRolesBoxInput.setValue("");
				mRolesBox.setValue(roles);
			}
		});

		clear.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				mRolesBox.setValue("");
			}
		});

		// create panels
		VerticalPanel main = new VerticalPanel();
		HorizontalPanel buttons = new HorizontalPanel();
		FlexTable form = new FlexTable();

		// Create input widgets
		mID = "";
		mFirstName = new TextBox();
		mLastName = new TextBox();
		mNickname = new TextBox();
		mAge = new TextBox();
		mRBSexMan = new RadioButton("sex", Constants.I18N.labelMan());
		mRBSexWoman = new RadioButton("sex", Constants.I18N.labelWoman());
		mRBSexMan.setValue(true);
		mRolesBoxInput = new SuggestBox(GuestsService.getRoles());
		mRolesBox = new TextBox();
		mRolesBox.setEnabled(false);
		mGroup = new SuggestBox();
		mComing = new ListBox();
		mComing.addItem(Constants.I18N.labelGuestDidNotAnswer(), "-1");
		mComing.addItem(Constants.I18N.labelGuestIsNotComing(), "0");
		mComing.addItem(Constants.I18N.labelGuestIsComing(), "1");
		mComingWith = new SuggestBox();

		// create form
		form.setText(0, 0, Constants.I18N.labelFirstName());
		form.setWidget(0, 1, mFirstName);
		form.setText(1, 0, Constants.I18N.labelLastName());
		form.setWidget(1, 1, mLastName);
		form.setText(2, 0, Constants.I18N.labelNickName());
		form.setWidget(2, 1, mNickname);
		form.setText(3, 0, Constants.I18N.labelAge());
		form.setWidget(3, 1, mAge);
		form.setWidget(4, 0, mRBSexMan);
		form.setWidget(4, 1, mRBSexWoman);
		form.setText(5, 0, Constants.I18N.labelRoles());
		form.setWidget(5, 1, mRolesBoxInput);
		form.setWidget(5, 2, add);
		form.setWidget(6, 1, mRolesBox);
		form.setWidget(6, 2, clear);
		form.setText(7, 0, Constants.I18N.labelGroup());
		form.setWidget(7, 1, mGroup);
		form.setText(8, 0, Constants.I18N.labelIsComing());
		form.setWidget(8, 1, mComing);
		form.setText(9, 0, Messages.I18N.comingWith(" ?"));
		form.setWidget(9, 1, mComingWith);

		// assemble panels
		buttons.add(mSaveButton);
		buttons.add(mCancelButton);

		// assemble main panel
		main.add(form);
		main.add(buttons);

		// add panel to dialog box
		add(main);
	}

	/**
	 * @see FormDisplay#getSaveButton()
	 */
	public HasClickHandlers getSaveButton() {
		return mSaveButton;
	}

	/**
	 * @see FormDisplay#getCancelButton()
	 */
	public HasClickHandlers getCancelButton() {
		return mCancelButton;
	}

	/**
	 * @see FormDisplay#displayGuest(Guest)
	 */
	public void displayGuest(Guest g) {
		if (g == null)
			return;

		mID = g.getID();
		mFirstName.setText(g.getFirstName());
		mLastName.setText(g.getLastName());
		mNickname.setText(g.getNickname());
		mAge.setText("" + g.getAge());
		if (g.isMan())
			mRBSexMan.setValue(true);
		else
			mRBSexWoman.setValue(true);
		mRolesBox.setValue(g.getRoles());
		mGroup.setValue(g.getGroup());
		mComing.setSelectedIndex(g.getStatus() + 1);
		mComingWith.setValue(g.getComingWith());
	}

	/**
	 * @see FormDisplay#getGuest()
	 */
	public Guest getGuest() {
		Guest guest = new Guest();
		guest.setID(mID);
		guest.setFirstName(mFirstName.getValue());
		guest.setLastName(mLastName.getValue());
		guest.setNickname(mNickname.getValue());
		guest.setAge(mAge.getValue());
		guest.setSex(mRBSexMan.getValue());
		guest.setRoles(mRolesBox.getValue());
		guest.setGroup(mGroup.getValue());
		guest.setStatus(mComing.getValue(mComing.getSelectedIndex()));
		guest.setComingWith(mComingWith.getValue());
		return guest;
	}

	/**
	 * @see FormDisplay#startForm()
	 */
	public void startForm() {
		center();
		mFirstName.setFocus(true);
	}

	/**
	 * @see FormDisplay#stopForm()
	 */
	public void stopForm() {
		hide();
	}

	/**
	 * @see FormDisplay#asWidget()
	 */
	public Widget asWidget() {
		return this;
	}

	/** the Save button */
	private Button mSaveButton;

	/** the Cancel button */
	private Button mCancelButton;

	/** The id of the guest */
	private String mID;

	/** The First Name */
	private TextBox mFirstName;

	/** The Last Name */
	private TextBox mLastName;

	/** The NickName */
	private TextBox mNickname;

	/** The age */
	private TextBox mAge;

	/** Radio Button Man */
	private RadioButton mRBSexMan;

	/** Radio Button Woman */
	private RadioButton mRBSexWoman;

	/** */
	private SuggestBox mRolesBoxInput;
	/** */
	private TextBox mRolesBox;

	/**  */
	private SuggestBox mGroup;

	/** */
	private ListBox mComing;

	/**  */
	private SuggestBox mComingWith;

}
